package com.jing21.learn.basic;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by 郑靖 on 2017/9/15.
 */
@SpringCloudApplication
@EnableSwagger2
public class BasicApplication implements BeanFactoryAware {

    public static void main(String[] args) {
        SpringApplication.run(BasicApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public Void apis() {

        ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;

        configurableBeanFactory.registerSingleton("pay", new Docket(DocumentationType.SWAGGER_2).groupName("pay").apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.jing21.learn.basic.controller"))
                .paths(PathSelectors.any())// 过滤的接口
                .build());

        configurableBeanFactory.registerSingleton("system", new Docket(DocumentationType.SWAGGER_2).groupName("system")
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.jing21.learn.basic.system.controller"))
                .paths(PathSelectors.any())// 过滤的接口
                .build());
        return null;
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("基础服务").description("基础服务").termsOfServiceUrl("")
                .contact(new Contact("zhengjing", "", "")).license("learn").version("0.0.1-SNAPSHOT").build();
    }

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}
